package controllers;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.EventService;
import services.ParticipantService;
import domain.Event;

@Controller
@RequestMapping("/event/administrator")
public class EventAdministratorController extends AbstractController {

	// Constructors -----------------------------------------------------------

	public EventAdministratorController() {
		super();
	}

	// Services ---------------------------------------------------------------

	@Autowired
	private EventService eventService;

	@Autowired
	private ParticipantService participantService;
	
	// List events created ----------------------------------------------------

	@RequestMapping(value = "/created", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Event> events;

		events = eventService.findEventsByAdministrator();
		result = new ModelAndView("event/created");
		result.addObject("requestURI", "event/administrator/created.do");
		result.addObject("events", events);
		result.addObject("showDetails", true);

		return result;
	}

	// List on-going events created --------------------------------------------

	@RequestMapping(value = "/on-goingcreated", method = RequestMethod.GET)
	public ModelAndView onGoingList() {
		ModelAndView result;
		Collection<Event> events;

		events = eventService.findOngoingEventsByAdministrator();
		result = new ModelAndView("event/on-goingcreated");
		result.addObject("requestURI", "event/administrator/on-goingcreated.do");
		result.addObject("events", events);
		result.addObject("showDetails", true);

		return result;
	}
	
	// Creation ---------------------------------------------------------------

			@RequestMapping(value = "/create", method = RequestMethod.GET)
			public ModelAndView create() {
				ModelAndView result;
				Event event;

				event = eventService.create();
				result = createEditModelAndView(event);

				return result;
			}
			
			protected ModelAndView createEditModelAndView(Event event) {
				ModelAndView result;

				result = createEditModelAndView(event, null);
				
				return result;
			}	
			
			protected ModelAndView createEditModelAndView(Event event, String message) {
				ModelAndView result;;
				result = new ModelAndView("event/edit");
				result.addObject("event",event);
				result.addObject("message",message);
				if(event.getId()==0)
					result.addObject("isNew",true);
				return result;
			}
			
			// Details ---------------------------------------------------------------
			
			@RequestMapping(value = "/details", method = RequestMethod.GET)
			public ModelAndView editEmployer(@RequestParam int eventId) {
				ModelAndView result;
				Event event = eventService.findOne(eventId);

				result = createEditModelAndView(event);
				result.addObject("readonly", !(event.getRegistrations().isEmpty()));
				result.addObject("participants", participantService.findParticipantsByEvent(event));
				return result;
			}
			
			@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
			public ModelAndView save(@Valid Event event, BindingResult binding) {
				ModelAndView result;

				if (binding.hasErrors()&&!eventService.itsHacked(event)) {
					if(binding.getErrorCount()!=2)
					{
					result = createEditModelAndView(event);
					}
					else
					{
						try {
							result = persist(event);
						} catch (Throwable oops) {
							result = createEditModelAndView(event, "event.commit.error");	
							
						}
					}
					System.out.println(binding.getAllErrors());
										
				} 
				else if(eventService.itsHacked(event))
				{
					result = createEditModelAndView(event);
				}
				else {
					try {
						result = persist(event);
					} catch (Throwable oops) {
						result = createEditModelAndView(event, "event.commit.error");	
						
					}
				}

				
				return result;
			}
					
			@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "delete")
			public ModelAndView delete(Event event, BindingResult binding) {
				ModelAndView result;
				
				try {
					Event toDelete = eventService.findOne(event.getId());
					eventService.delete(toDelete);
					result = new ModelAndView("redirect:created.do");						
				} catch (Throwable oops) {
					result = createEditModelAndView(event, "event.commit.error");
				}

				return result;
			}
		
		private ModelAndView persist(Event e){
			ModelAndView res;
			Event toPersist;
			if(e.getId()!=0){
			// reconstruct entity
			toPersist = eventService.findOne(e.getId());
			toPersist.setTitle(e.getTitle());
			toPersist.setFee(e.getFee());
			toPersist.setStartMoment(e.getStartMoment());
			toPersist.setFinishMoment(e.getFinishMoment());
			toPersist.setDescription(e.getDescription());
			toPersist.setRating(e.getRating());
			}
			
			else
			{
				toPersist = eventService.create();
				toPersist.setTitle(e.getTitle());
				toPersist.setFee(e.getFee());
				toPersist.setStartMoment(e.getStartMoment());
				toPersist.setFinishMoment(e.getFinishMoment());
				toPersist.setDescription(e.getDescription());
			}
			
			// persist entity
			eventService.save(toPersist);				
			res = new ModelAndView("redirect:created.do");
			
			return res;
		}
}
